package com.jt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.pojo.User;
import com.jt.service.DubboUserService;
import com.jt.service.HttpClientService;
import com.jt.util.CookieUtil;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.JedisCluster;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Cx
 * @stage JT Of Fourth stage
 * @creat 2020-12-11 11:45
 */
@Controller
@RequestMapping("/user/")
public class UserController {

    // 定义cookie常量
    private static final String JT_TICKET = "JT_TICKET";

    @Autowired()
    private HttpClientService httpClientService;
    @Reference(check = false)
    private DubboUserService userService;
    @Autowired
    private JedisCluster jedisCluster;

    // 实现用户页面通用跳转 http://www.jt.com/user/login.html register.html
    @RequestMapping("{moduleName}")
    public String moduleName(@PathVariable String moduleName) {
        return moduleName;
    }

    @RequestMapping("findUserList")
    @ResponseBody
    public List<User> findUserList() {
        List<User> userList = httpClientService.findUserList();
        return userList;
    }

    // 实现用户操作 /user/doRegister
    @RequestMapping("doRegister")
    @ResponseBody
    public SysResult saveUser(User user) {
        userService.saveUser(user);
        return SysResult.success();
    }

    // 实现单点登陆操作 doLogin
    @RequestMapping("doLogin")
    @ResponseBody
    public SysResult userLogin(User user, HttpServletResponse response) {
        String ticket = userService.findUserByUP(user);
        if (!StringUtils.hasLength(ticket)) {
            // 如果数据为空 直接返回错误信息
            return SysResult.fail();
        }
        // 有值将数据保存到cookie中
        Cookie cookie = new Cookie("JT_TICKET", ticket);
        cookie.setPath("/"); // 设定权限访问地址
        cookie.setMaxAge(7 * 24 * 60 * 60); // 设定有效期
        cookie.setDomain("jt.com"); // 域名包含jt.com则可以共享数据
        response.addCookie(cookie);
        return SysResult.success();
    }

    // 实现用户退出
    @RequestMapping("logout")
    public String logout(
            HttpServletRequest request,
            HttpServletResponse response) { // 用request获取cookie
        String ticket = CookieUtil.getCookieValue(request, JT_TICKET);
        if (StringUtils.hasLength(ticket)) {
            jedisCluster.del(ticket);
            CookieUtil.addCookie(
                    response,
                    JT_TICKET,
                    "",
                    "/",
                    "jt.com",
                    0);
        }
        return "redirect:/"; // 重定向到系统首页

/*        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (JT_TICKET.equals(cookie.getName())) {
                    String ticket = cookie.getValue();
                    // 删除redis数据
                    jedisCluster.del(ticket);
                    // 删除cookie数据
                    cookie.setPath("/"); // 设定权限访问地址
                    cookie.setMaxAge(0); // 设定有效期 0为立即删除 -1为关闭浏览器后删除
                    cookie.setDomain("jt.com"); // 域名包含jt.com则可以共享数据
                    response.addCookie(cookie);
                }
            }
        }*/
    }

}
